package com.erpoa.controller;

import com.alibaba.fastjson.JSON;
import com.erpoa.dto.DealMessage;
import com.erpoa.entity.PackMessageByFlatStorage;
import com.erpoa.service.FlatStorageService;
import com.erpoa.utils.PDAException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;


/**
 * @description:
 * @author: jamin.chen
 * @createDate: 2020/1/3 9:37
 * @version: 1.0
 */
@CrossOrigin(maxAge = 3600)
@Api(value = "flatStorageController", tags = {"物流中心133亩地2号仓接口"})
@RestController
@RequestMapping("/PDAService/flatStorage")
public class FlatStorageController {
    private static final Logger logger = LoggerFactory.getLogger(FlatStorageController.class);


    @Autowired
    private FlatStorageService flatStorageService;


    /**
     * wuLiuFahuoServlet
     *
     * @return
     * @throws Exception
     */
    @ApiOperation("物流中心133亩地2号仓接口")
    @RequestMapping(value = {"/flatStorageServlet"}, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public DealMessage flatStorageServlet(@RequestBody PackMessageByFlatStorage packMessage) {
        DealMessage dealMessage = new DealMessage(true);
        int optType = packMessage.getOptType();

        try {
            logger.debug("线程:" + Thread.currentThread().getId() + "请求开始：" + packMessage.toString());
            switch (optType) {
                //单号接口
                case 0:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.querylistNo(packMessage.getListNo())));
                    break;
                //条码接口
                case 1:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.querypckBarcode(packMessage)));
                    break;
                //已组盘接口
                case 2:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.querypckpackMessage(packMessage.getListNo(), packMessage.getPalletNo())));
                    break;
                //单位接口
                case 3:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.querypckUn(packMessage.getProdNo(), packMessage.getOrgNo())));
                    break;
                //项次接口
                case 4:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.querylineId(packMessage.getListNo())));
                    break;
                //根据单位和项次和料号查询
                case 5:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.querybyflat(packMessage)));
                    break;
                //组盘确认
                case 6:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.insert(packMessage)));
                    break;
                //组盘取消接口
                case 7:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.deletepalletNo(packMessage)));
                    break;
                //组盘详情接口
                case 8:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.qureyLINE(packMessage)));
                    break;
                //组盘取消单号查询
                case 9:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.querydetalt(packMessage.getListNo())));
                    break;
                //上架作业扫描托盘号带出明细
                case 45:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.groundQueryDetailByPalletNo(packMessage)));
                    break;
                //上架作业储区验证
                case 44:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.groundQueryAreaNoByPalletNo(packMessage)));
                    break;
                //上架作业正式操作
                case 43:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.groundUpdateDetailByPalletNo(packMessage)));
                    break;
                //出库作业储区下拉
                case 42:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.selectAreaListByParam(packMessage)));
                    break;
                //出库作业客户名称下拉
                case 41:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.selectSnameListByParam(packMessage)));
                    break;
                //出库作业出库列表
                case 40:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.selectOutListByParam(packMessage)));
                    break;
                //出库作业根据外箱码带出料号作比对
                case 39:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.checkProdNoBypckCode(packMessage)));
                    break;
                //出库作业根据确认完结并返回物流码明细列表
                case 38:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.insertOut(packMessage)));
                    break;
                //出口与明细
                case 37:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.selectOutList(packMessage)));
                    break;
                //
                case 36:
                    dealMessage.setMessage(JSON.toJSONString(flatStorageService.updateAutoPrint(packMessage)));
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof PDAException) {
                dealMessage = new DealMessage(false, e.getMessage());
            } else if (e instanceof DuplicateKeyException) {
                throw new PDAException("数据重复，请重新扫描！");

            } else if (e instanceof TooManyResultsException) {
                dealMessage = new DealMessage(false, "数据错误，请检查！" + e.getMessage());

            } else if (e instanceof SQLException) {
                throw new PDAException("数据重复，请重新扫描！");
            } else {
                dealMessage = new DealMessage(false, "数据处理出现未知错误，请再扫一次确认结果，如多次出现请及时反馈IT，分机号：6257！时间点：" + System.currentTimeMillis() + "错误信息：" + e.getMessage());
            }
            logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            return dealMessage;
        }
        logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
        return dealMessage;
    }


}
